<?php

namespace backend\controllers;


use backend\models\EveryIsOk;
use backend\models\GoodsCategory;
use yii\base\Exception;
use yii\bootstrap\Html;
use yii\web\Controller;

class GoodsCategoryController extends Controller
{

    /**
     * 添加行为控制过滤
     * @return array
     */
    public function behaviors()
    {
        return [
            'access'=>[
                'class'=>'backend\filter\AccessFilter',
            ],
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    /*
     * 添加分类
     */
    public function actionAdd(){
        //实例化表单模型
        $model = new GoodsCategory();
        //加载并且验证数据
        if($model->load(\Yii::$app->request->post()) && $model->validate()){
            //调用方法添加节点
           $rs = $model->addNode();
            //提示
            EveryIsOk::setTisiInfo($rs,'添加分类');
            //刷新
            return $this->refresh();

        }
        //显示视图
        return $this->render('add',['model'=>$model]);
    }

    /**
     * 测试
     */
    public function actionAddNode(){
        $category = new GoodsCategory(['name' => 'Countries']);
        $category->parent_id = 0;
        /** @var $category  \creocoder\nestedsets\NestedSetsBehavior */
        $category->makeRoot();

        $catechild = new GoodsCategory(['name' => 'Russia']);
        $catechild->parent_id  = $category->id;
        /** @var $catechild  \creocoder\nestedsets\NestedSetsBehavior */
        $catechild->prependTo($category);
    }

    public function actionTest(){
        return $this->renderPartial('test');
    }

    public function actionList(){
        //获取所有的分类
        $query = GoodsCategory::find();
        //得到分页对象
        $pager = EveryIsOk::getPager($query,20);
        //执行查询
        $models = $query->limit($pager->limit)->offset($pager->offset)->orderBy(['tree'=>'SORT_DESC','lft'=>'SORT_ASC'])->all();
        //显示 视图
        return $this->render('list',['models'=>$models,'pager'=>$pager]);

    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     */
    public function actionEdit($id){
        //实例化表单模型
        $model = GoodsCategory::findOne(['id'=>$id]);
        //如果数据不存在,提示用户并且放回列表页面
        if(empty($model)){
            \Yii::$app->session->setFlash(false,'数据不存在');
            return $this->redirect(['goods-category/list']);
        }
        //加载并且验证数据
        if($model->load(\Yii::$app->request->post()) && $model->validate()){
            //调用方法添加节点
            $rs = $model->addNode();
            //提示
            EveryIsOk::setTisiInfo($rs,'编辑分类');
            //成功后刷新页面
            return $this->redirect(['goods-category/list']);

        }
        //显示视图
        return $this->render('add',['model'=>$model]);
    }


    public function actionDel($id){
        //实例化表单模型
        $model = GoodsCategory::findOne(['id'=>$id]);
        try{
            //执行删除
            $model->delete();
        }catch (Exception $exception){
            //提示
            \Yii::$app->session->setFlash('danger','顶级节点不能删除'.Html::a('<strong>  强制删除 </strong>',['goods-category/delete','id'=>$id]));
        }finally{
            //跳转
            return $this->redirect(['goods-category/list']);
        }

    }

    /*
   * 实现商品分类的彻底删除
   */
    public function actionDelete($id){
        //执行删除
        $rs = \Yii::$app->db->createCommand()->delete('goods_category',['id'=>$id])->execute();
        //提示
        EveryIsOk::setTisiInfo($rs,'彻底删除');
        //跳转
        return $this->redirect(['goods-category/list']);

    }

}
